package com.small.admin.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.small.admin.config.constant.MsgAdmin;
import com.small.admin.dto.AdminDelete;
import com.small.admin.dto.AdminModifyPwd;
import com.small.admin.dto.AdminPage;
import com.small.admin.dto.AdminStore;
import com.small.admin.entity.RoleAdmin;
import com.small.admin.mapper.RoleAdminMapper;
import com.small.common.msg.Msg;
import com.small.common.msg.MsgPage;
import com.small.common.util.encrypt.MD5;
import org.springframework.stereotype.Service;
import com.small.common.base.service.impl.BaseServiceImpl;
import com.small.admin.entity.Admin;
import com.small.admin.mapper.AdminMapper;
import com.small.admin.service.AdminService;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;

/**
 * 作者: fenny
 * 邮箱: 893146555@qq.com
 * 时间: 2017-10-17 15:28:29
 * 描述: 管理员信息 实现
 */
@Service
public class AdminServiceImpl extends BaseServiceImpl<AdminMapper, Admin> implements AdminService {

    @Resource
    private RoleAdminMapper roleAdminMapper;

    @Override
    public Admin selectByAccount(String account) {
        return mapper.selectByAccount(account);
    }

    @Override
    public Msg modifyPwd(AdminModifyPwd adminModifyPwd, long adminId) {
        Admin adminW = new Admin();
        adminW.setId(adminId);
        Admin admin = mapper.selectOne(adminW);
        if (admin.getPassword().equals(MD5.encrypt32(adminModifyPwd.getSource()))) {
            String newPwd = MD5.encrypt32(adminModifyPwd.getPassword());
            adminW.setPassword(newPwd);
            adminW.setUid(adminId);
            adminW.setUtime(System.currentTimeMillis());
            adminW.setIsSystem(null);
            updateSelectiveById(adminW);
            return MsgAdmin.SUCCESS;
        }
        return MsgAdmin.TIP_SOURCE_PASSWORD_ERROR;
    }

    @Override
    public int add(AdminStore admin) {
        int count = insert(admin);
        if (admin.getRoleIds() != null && admin.getRoleIds().length > 0) {
            //添加用户的同时,给用户指定角色
            RoleAdmin roleAdmin = new RoleAdmin();
            for (long roleId : admin.getRoleIds()) {
                roleAdmin.setRoleId(roleId);
                roleAdmin.setAdminId(admin.getId());
                roleAdminMapper.insert(roleAdmin);
            }
        }
        return count;
    }

    @Override
    public void update(AdminStore admin) {
        admin.setIsSystem(null);
        int result = updateSelectiveById(admin);
        if (result > 0) {
            //先清空当前用户的角色关联
            RoleAdmin roleAdminWhere = new RoleAdmin();
            roleAdminWhere.setAdminId(admin.getId());
            roleAdminMapper.delete(roleAdminWhere);
            //添加用户关联的角色
            if (admin.getRoleIds() != null) {
                RoleAdmin roleAdmin = new RoleAdmin();
                for (long roleId : admin.getRoleIds()) {
                    roleAdmin.setRoleId(roleId);
                    roleAdmin.setAdminId(admin.getId());
                    roleAdminMapper.insert(roleAdmin);
                }
            }
        }
    }

    @Override
    public MsgPage<Admin> selectByPage(AdminPage adminPage) {
        Page<Object> result = PageHelper.offsetPage(adminPage.getOffset(), adminPage.getLimit());
        MsgPage<Admin> page = new MsgPage<>(mapper.selectByPage(adminPage));
        page.setTotal(result.getTotal());
        return page;
    }

    @Override
    public void deletes(AdminDelete adminDelete) {
        //删除用户
        Example example = new Example(Admin.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andIn("id", adminDelete.getIds());
        //超级管理员禁止删除
        criteria.andNotEqualTo("isSystem", 1);
        //加入创建人作为删除条件
        if (adminDelete.getCid() != null && adminDelete.getCid() > 0) {
            criteria.andEqualTo("cid", adminDelete.getCid());
        }
        int result = mapper.deleteByExample(example);
        if (result > 0) {
            //删除用户关联的角色
            Example roleAdminEx = new Example(RoleAdmin.class);
            Example.Criteria roleAdminCr = roleAdminEx.createCriteria();
            roleAdminCr.andIn("adminId", adminDelete.getIds());
            roleAdminMapper.deleteByExample(roleAdminEx);
        }
    }
}